package com.example.petshopping.ui.aquarium;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petshopping.R;
import com.example.petshopping.ui.fixedprice.FixedPriceActivity;
import com.example.petshopping.ui.main.MainActivity;
import com.example.petshopping.ui.pet.PetActivity;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class AquariumActivity extends AppCompatActivity {
    
    private RecyclerView aquariumProductList;
    private List<AquariumProduct> productList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aquarium);
        
        // 设置状态栏高度
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            int statusBarHeight = getResources().getDimensionPixelSize(resourceId);
            findViewById(R.id.status_bar_bg).getLayoutParams().height = statusBarHeight;
        }
        
        // 设置导航标签的点击监听
        setupNavigation();
        
        // 初始化商品列表
        initProductList();

        // 搜索输入框点击跳转到搜索页
        findViewById(R.id.search_input).setOnClickListener(v -> {
            Intent intent = new Intent(AquariumActivity.this, com.example.petshopping.ui.search.SearchActivity.class);
            startActivity(intent);
        });
        
        // 设置底部导航栏
        setupBottomNavigation();
    }
    
    private void initProductList() {
        // 准备数据
        productList = new ArrayList<>();
        productList.add(new AquariumProduct("豪华鱼缸套装", "适合新手的全套装备", "¥599.00", R.drawable.aquarium_1));
        productList.add(new AquariumProduct("热带鱼饲料", "高蛋白营养配方", "¥39.90", R.drawable.aquarium_2));
        productList.add(new AquariumProduct("水质净化器", "保持水质清澈", "¥129.00", R.drawable.aquarium_3));
        productList.add(new AquariumProduct("专业水草套装", "打造自然水草景观", "¥159.00", R.drawable.aquarium_4));
        
        // 设置RecyclerView
        aquariumProductList = findViewById(R.id.aquarium_product_list);
        aquariumProductList.setLayoutManager(new GridLayoutManager(this, 2));
        aquariumProductList.setAdapter(new AquariumProductAdapter(productList));
    }
    
    private void setupNavigation() {
        TabLayout navMenu = findViewById(R.id.nav_menu);
        navMenu.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                // 如果不是点击当前页面对应的标签，则进行页面跳转
                if (position != 2) { // 水族标签的位置是2
                    switch (position) {
                        case 0: // 首页
                            startActivity(new Intent(AquariumActivity.this, MainActivity.class));
                            overridePendingTransition(0,0);
                            finish();
                            break;
                        case 1: // 宠物
                            startActivity(new Intent(AquariumActivity.this, PetActivity.class));
                            overridePendingTransition(0,0);
                            finish();
                            break;
                        case 3: // 一口价
                            startActivity(new Intent(AquariumActivity.this, FixedPriceActivity.class));
                            overridePendingTransition(0,0);
                            finish();
                            break;
                        case 4: // 同城
                            startActivity(new Intent(AquariumActivity.this, com.example.petshopping.ui.samecity.SameCityActivity.class));
                            overridePendingTransition(0,0);
                            finish();
                            break;
                    }
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                // 不需要处理
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                // 不需要处理
            }
        });
        
        // 设置默认选中的标签为水族
        navMenu.getTabAt(2).select();
    }
    
    private void setupBottomNavigation() {
        // 获取底部导航栏
        com.google.android.material.bottomnavigation.BottomNavigationView bottomNav = findViewById(R.id.bottom_nav);
        
        // 设置当前选中项为首页(因为水族是顶部标签，底部没有对应标签)
        bottomNav.setSelectedItemId(R.id.navigation_home);
        
        // 设置点击监听器
        bottomNav.setOnNavigationItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    // 跳转到首页
                    startActivity(new Intent(this, MainActivity.class));
                    overridePendingTransition(0,0);
                    finish();
                    return true;
                case R.id.navigation_category:
                    // 跳转到分类页面
                    startActivity(new Intent(this, com.example.petshopping.ui.category.CategoryActivity.class));
                    overridePendingTransition(0,0);
                    finish();
                    return true;
                case R.id.navigation_message:
                    // 跳转到消息页面
                    startActivity(new Intent(this, com.example.petshopping.ui.message.MessageActivity.class));
                    overridePendingTransition(0,0);
                    finish();
                    return true;
                case R.id.navigation_profile:
                    // 跳转到我的页面
                    startActivity(new Intent(this, com.example.petshopping.ui.profile.ProfileActivity.class));
                    overridePendingTransition(0,0);
                    finish();
                    return true;
            }
            return false;
        });
        
        // 设置中间按钮点击事件
        findViewById(R.id.center_button).setOnClickListener(v -> {
            // 中间按钮点击事件
            // 可以根据需要添加功能
        });
    }
    
    private int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
    
    // 水族产品模型类
    private static class AquariumProduct {
        private String name;
        private String description;
        private String price;
        private int imageResId;
        
        public AquariumProduct(String name, String description, String price, int imageResId) {
            this.name = name;
            this.description = description;
            this.price = price;
            this.imageResId = imageResId;
        }
    }
    
    // 水族产品适配器
    private class AquariumProductAdapter extends RecyclerView.Adapter<AquariumProductAdapter.ViewHolder> {
        private List<AquariumProduct> products;
        
        public AquariumProductAdapter(List<AquariumProduct> products) {
            this.products = products;
        }
        
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_aquarium_product, parent, false);
            return new ViewHolder(view);
        }
        
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            AquariumProduct product = products.get(position);
            holder.productName.setText(product.name);
            holder.productPrice.setText(product.price);
            holder.productImage.setImageResource(product.imageResId);
        }
        
        @Override
        public int getItemCount() {
            return products.size();
        }
        
        class ViewHolder extends RecyclerView.ViewHolder {
            ImageView productImage;
            TextView productName, productPrice;
            
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                productImage = itemView.findViewById(R.id.aquarium_product_image);
                productName = itemView.findViewById(R.id.aquarium_product_name);
                productPrice = itemView.findViewById(R.id.aquarium_product_price);
            }
        }
    }
} 